package com.lincoln.redis.server.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.lincoln.redis.server.bean.BaseEntity;
import com.lincoln.redis.server.config.PermissionConfig;
import com.lincoln.redis.server.utils.SpringBeanFactoryUtils;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * 用户类
 *
 * @author lincoln
 */
@Data
public class User extends BaseEntity implements UserDetails {
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;

    private boolean enabled;

    private boolean accountNonLocked;

    private boolean accountNonExpired;

    private boolean credentialsNonExpired;

    /**
     * 每个用户都有的权限
     */
    private Set<String> permissions;

    @JSONField(serialize = false)
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> authorities = new HashSet<>();
        if(permissions == null){
            return authorities;
        }
        for (String base : permissions) {
            authorities.add(new SimpleGrantedAuthority(base));
        }
        return authorities;
    }
}
